package com.hjl.Utils;

import java.sql.*;
import java.util.Collection;

/**
 * @author ServivE
 * @version 0.1
 * @className DBUtil
 * @description
 * @date 2024/7/23 15:28
 * @since jdk11
 */
public class DBUtil {

    public static Connection getConnection(){
        Connection conn = null;
       //获取连接
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///atguigu","root","123456");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    //关闭连接
    public static void closeAll(ResultSet res, Statement statement, Connection connection) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
